package ee.maoinvest.frontend.controller.command.electricitymetercommands;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.model.data.ElectricityMeterAndItsElectricityContractPeriod;
import ee.maoinvest.backend.model.data.views.LocalTerritoryElectricityMeter;
import ee.maoinvest.backend.services.ElectricityMeterService;
import ee.maoinvest.backend.services.EnterpriseRelationsWithElectricityMetersAndContractsService;
import ee.maoinvest.backend.services.implementations.ElectricityMeterServiceImpl;
import ee.maoinvest.backend.services.implementations.EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.electricitymeterforms.ReplaceOrDeleteLocalTerritoryElectricityMeterFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.ElectricityMeterFormsRequestProcessor;

public class GetAndFillReplaceOrDeleteLocalTerritoryElectricityMeterFormWithInitialDataCommand implements Command
{
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	
	private final int ID_PARSE_ERROR_IDENTIFICATOR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		ElectricityMeterFormsRequestProcessor electricityMeterFormsRequestProcessor = null;
		ReplaceOrDeleteLocalTerritoryElectricityMeterFormConverter replaceOrDeleteLocalTerritoryElectricityMeterFormConverter = null;
		LocalTerritoryElectricityMeter localTerritoryElectricityMeter = null;
		ElectricityMeterService electricityMeterService = null;
		List<ElectricityMeter> listOfPotentialParentElectricityMetersForLocalTerritoryMeter = null;
		List<ElectricityMeter> listOfRelatedChildElectricityMeters = null;
		EnterpriseRelationsWithElectricityMetersAndContractsService enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = null;
		ElectricityMeterAndItsElectricityContractPeriod electricityMeterAndItsElectricityContractPeriod = null;
		String contractNameMadeOnMeter = null;
		String enterpriseNameMeterIsHavingContractWith = null;
		electricityMeterFormsRequestProcessor = new ElectricityMeterFormsRequestProcessor();
		
		int localTerritoryElectricityMeterIDToReplace = 
				electricityMeterFormsRequestProcessor.getLocalTerritoryElectricityMeterIDToReplaceOrDelete(request);
		
		if (localTerritoryElectricityMeterIDToReplace != ID_PARSE_ERROR_IDENTIFICATOR)
		{
			electricityMeterService = new ElectricityMeterServiceImpl();
			listOfPotentialParentElectricityMetersForLocalTerritoryMeter = new ArrayList<ElectricityMeter>();
			listOfRelatedChildElectricityMeters = new ArrayList<ElectricityMeter>();
			listOfRelatedChildElectricityMeters = new ArrayList<ElectricityMeter>();
			enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = 
					new EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl();
			
			localTerritoryElectricityMeter = electricityMeterService.getLocalTerritoryElectricityMeterByID(
					localTerritoryElectricityMeterIDToReplace);
			listOfPotentialParentElectricityMetersForLocalTerritoryMeter = 
					electricityMeterService.getListOfPotentialParentElectricityMeters(
					localTerritoryElectricityMeterIDToReplace);
			listOfRelatedChildElectricityMeters = electricityMeterService.getListOfRelatedChildElectricityMeters(
					localTerritoryElectricityMeterIDToReplace);
			electricityMeterAndItsElectricityContractPeriod = 
					enterpriseRelationsWithElectricityMetersAndContractsServiceImpl.getElectricityMeterAndItsElectricityContractPeriodByElectricityMeterID(
							localTerritoryElectricityMeterIDToReplace);
			contractNameMadeOnMeter = electricityMeterService.getContractNameMadeOnMeter(
					localTerritoryElectricityMeterIDToReplace);
			enterpriseNameMeterIsHavingContractWith = electricityMeterService.getEnterpriseNameMeterIsHavingContractWith(
					localTerritoryElectricityMeterIDToReplace);
			
			if (localTerritoryElectricityMeter != null)
			{
				replaceOrDeleteLocalTerritoryElectricityMeterFormConverter = 
						new ReplaceOrDeleteLocalTerritoryElectricityMeterFormConverter();
				replaceOrDeleteLocalTerritoryElectricityMeterFormConverter.fillWithInitialData(
						localTerritoryElectricityMeter, listOfPotentialParentElectricityMetersForLocalTerritoryMeter, 
						listOfRelatedChildElectricityMeters,
						electricityMeterAndItsElectricityContractPeriod, enterpriseNameMeterIsHavingContractWith, 
						contractNameMadeOnMeter);
				
				request.setAttribute("replaceOrDeleteLocalTerritoryElectricityMeterFormConverter", 
						replaceOrDeleteLocalTerritoryElectricityMeterFormConverter);
				
				HttpSession session = request.getSession();
				session.setAttribute("localTerritoryElectricityMeter", localTerritoryElectricityMeter);
				session.setAttribute("listOfPotentialParentElectricityMetersForLocalTerritoryMeter", 
						listOfPotentialParentElectricityMetersForLocalTerritoryMeter);
				session.setAttribute("listOfRelatedChildElectricityMeters", listOfRelatedChildElectricityMeters);
	
				operationResult = OPERATION_SUCCESSFUL;
			}
		}
		else
		{
			operationResult = OPERATION_UNSUCCESSFUL;
		}

		return operationResult;
	}
}